﻿Public Class frmSalaryCode

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ClearText()
    End Sub
    Private Sub ClearText()
        Me.txtID.Text = ""
        Me.txtName.Text = ""
        Me.txtNote.Text = ""
        Me.txtName.Focus()
        Me.txtID.Text = AutoID4Access("SELECT SalaryCodeID FROM tbl_salarycode", "tbl_salarycode") + 1
        LoadDataToLVSalaryCode("SELECT * FROM tbl_salarycode", LV)

        Me.btbUpdate.Enabled = False
        Me.btnSave.Enabled = True
    End Sub

    Private Sub frmSalaryCode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connect()

        Me.txtID.Text = AutoID4Access("SELECT SalaryCodeID FROM tbl_salarycode", "tbl_salarycode") + 1
        LoadDataToLVSalaryCode("SELECT * FROM tbl_salarycode", LV)

        Me.btbUpdate.Enabled = False
        Me.btnSave.Enabled = True
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Me.txtID.Text = "") Then
            MsgBox("Please Enter Value !", MsgBoxStyle.Exclamation, "Invalid Value")
            Me.txtID.Focus()
        ElseIf (Me.txtName.Text = "") Then
            MsgBox("Please Enter Value!", MsgBoxStyle.Exclamation, "Invalid Value")
            Me.txtName.Focus()
        Else
            Dim SQL As String = "INSERT INTO tbl_salarycode(SalaryCodeID,SalaryCode,Description)" & _
                              "VALUES (" & Me.txtID.Text & ",'" & Me.txtName.Text & "','" & txtNote.Text & "')"
            SaveDataToAccess(SQL)
            ClearText()
            LoadDataToLVSalaryCode("SELECT * FROM tbl_salarycode", LV)
            Me.txtID.Text = AutoID4Access("SELECT SalaryCodeID FROM tbl_salarycode", "tbl_salarycode") + 1
        End If
    End Sub

    Private Sub LV_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LV.MouseDoubleClick
        Dim i As Integer
        For i = 0 To Me.LV.Items.Count - 1
            If LV.Items(i).Selected = True Then
                txtID.Text = LV.Items(i).Text
                Me.txtName.Text = LV.Items(i).SubItems(1).Text
                Me.txtNote.Text = LV.Items(i).SubItems(2).Text
            End If
        Next
        Me.btbUpdate.Enabled = True
        Me.btnSave.Enabled = False
    End Sub

    Private Sub LV_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LV.SelectedIndexChanged

    End Sub

    Private Sub btbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbUpdate.Click

        Dim SQL As String = "UPDATE tbl_salarycode SET SalaryCode='" & Me.txtName.Text & _
                                         "' ,Description='" & txtNote.Text & "' WHERE SalaryCodeID=" & Me.txtID.Text
        SaveDataToAccess(SQL)
        LoadDataToLVSalaryCode("SELECT * FROM tbl_salarycode", LV)
        ClearText()

    End Sub

End Class